﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            using (AdminDb contexr = new AdminDb())
            {
                contexr.Database.Initialize(true);

                contexr.Users.Add(new Users
                {
                    UserName = "admin",
                    Password = "123",
                    IsActived = true,
                    IsDeleted = false,
                    CreatedTime = DateTime.Now,
                    UpdateTime = DateTime.Now
                });
                contexr.SaveChanges();
            }





            //Func传入参数
            Func<int, int, int> func = Say;
            var a = func(4, 6);
            Console.WriteLine(a);

            //Func and 匿名函数
            Func<string, int, string[]> extractMeth = delegate (string s, int i)
            {
                char[] delimiters = new char[] { ' ' };
                return i > 0 ? s.Split(delimiters, i) : s.Split(delimiters);
            };

            string b = "The Scarlet Letter";

            foreach (string word in extractMeth(b, 5))
                Console.WriteLine(word);

            //Func and lambda表达式
            char[] separators = new char[] { ' ' };
            Func<string, int, string[]> extract = (s, i) =>
                 i > 0 ? s.Split(separators, i) : s.Split(separators);

            string title = "go shopping";

            foreach (string word in extract(title, 5))
                Console.WriteLine(word);

            //Action与Func的用法差不多，差别只是一个有返回类型，一个没有返回类型，当然Action也可以接匿名方法和Lambda表达式
            Action hello = Hello;
            hello();

            //Action 匿名方法
            Action<string> hi = delegate (string word)
            {
                Console.WriteLine(word);
            };
            hi("hi ,everybody");

            //Action lambda表达式
            Action<string> oh = s =>
            {
                Console.WriteLine(s);
            };
            oh("oh my gold!!!");

            //委托Delegate
            Work work = working;
            Console.WriteLine(working());

            //Expression Lambda表达式创建了一个用于验证Name的Func委托
            Func<SearchInfo, bool> e = x => x.Name.Equals("5");
            bool res = e(new SearchInfo() { Name = "6" });
            Console.WriteLine(res);

            //Expression封装之后 调用
            bool f1 = We("6")(new SearchInfo() { Name = "6" });
            bool f2 = We("7")(new SearchInfo() { Name = "6" });
            bool f3 = We("8")(new SearchInfo() { Name = "6" });
            Console.WriteLine(f1);
            Console.WriteLine(f2);
            Console.WriteLine(f3);


            Expression<Func<SearchInfo, bool>> lambda = x => x.Name.Equals("5");

            bool w = lambda.Compile()(new SearchInfo() { Name = "6" });
            Console.WriteLine(w);

            Console.Read();
        }


        //Expression封装
        public static Func<SearchInfo, bool> We(string arr)
        {
            return x => x.Name.Contains(arr);
        }

        //Func传入参数
        private static int Say(int num1, int num2)
        {
            return num1 + num2;
        }

        //Action
        private static void Hello()
        {
            Console.WriteLine("Hello World!!!");
        }

        //委托 delegate
        private delegate string Work();

        public static string working()
        {
            return "努力工作";
        }
    }
}

